﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Text;
using System.IO;

public partial class Uploadclass : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Upload();            
    }

    public void Upload()
    {
        if (this.iconUpload.PostedFile != null && this.iconUpload.PostedFile.ContentLength > 0)
        {
            int UploadedLength = 0;
            int ContentLength = this.iconUpload.PostedFile.ContentLength;

            string path = this.Server.MapPath(@"Files");
            string fileName = Path.GetFileName(this.iconUpload.PostedFile.FileName);

            int bufferSize = 1;
            byte[] buffer = new byte[bufferSize];

            try
            {
                using (FileStream fs = new FileStream(Path.Combine(path, fileName), FileMode.Create))
                {
                    while (UploadedLength < ContentLength)
                    {
                        //Fill the buffer from the input stream
                        int bytes = this.iconUpload.PostedFile.InputStream.Read(buffer, 0, bufferSize);

                        //Writing the bytes to the file stream
                        fs.Write(buffer, 0, bytes);
                        UploadedLength += bytes;
                    }
                }
            }
            catch (Exception ex)
            {
                Response.Write("<script>alert('"+ ex.Message.ToString()+"');</script>");
            }
        }
    }
}